package com.businessportal.web.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by Vadim on 6/27/14.
 */
public class SettingsConfig {
    private static volatile SettingsConfig INSTANCE;

    private static final String PROPERTIES_FILE =
            "Resource.properties";

    private void load() throws IOException {
        Properties properties = new Properties();
        InputStream stream = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
        properties.load(stream);

        IsRemoteDeployment = Boolean.parseBoolean(properties.getProperty("remote.deployment", "false"));
        uploadBaseDirRemote = properties.getProperty("files.upload.path.remote", "");
        uploadBaseDirLocal = properties.getProperty("files.upload.path.local", "D:\\Share\\uploads");
    }

    private  SettingsConfig() {
        try {
            load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SettingsConfig getInstance(){
        if(INSTANCE == null){
            synchronized(SettingsConfig.class){
                //double checking Singleton instance
                if(INSTANCE == null){
                    INSTANCE = new SettingsConfig();
                }
            }
        }
        return INSTANCE;
    }

    private boolean IsRemoteDeployment = false;
    private String uploadBaseDirRemote;
    private String uploadBaseDirLocal;

    public boolean IsRemoteDeployment(){
        return IsRemoteDeployment;
    }

    public String getUploadBaseDir(){
        return IsRemoteDeployment ? uploadBaseDirRemote : uploadBaseDirLocal;
    }

}
